<?php
/**
 * Class to handle dates.
 */
class Date {
    private $timestamp;

    /**
     * Construct a date object.
     * If no string is provided the current date and time will be used.
     * @param string $datestr
     * @throws Date_Exception
     */
    public function __construct($datestr = null) {
        if(null !== $datestr) {
            $this->timestamp = strtotime($datestr);
            if(false === $this->timestamp) {
                throw new Date_Exception("'$datestr' is not a valid date.");
            }
        } else {
            $this->timestamp = mktime();
        }
    }

    /**
     * Get the date as a string.
     * @return string
     */
    public function getDateAsString($format = null) {
        if(null === $format) {
            $format = Framework::getConfig()->getSection('general')->dateformat;
        }
        return date($format, $this->timestamp);
    }
}
?>
